package com.example.itshome.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.impl.ViewOutlineProviderImpl;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.CollectCommodityRequest;

public class CommodityDialog extends Dialog {

    private final Context context;
    private final CommodityEntity commodity;
    private ImageView imageView;
    private TextView nameTextView;
    private TextView introductionTextView;
    private Button collectButton;
    private Button outSideCollectButton;

    public CommodityDialog setOutSideCollectButton(Button outSideCollectButton) {
        this.outSideCollectButton = outSideCollectButton;
        return this;
    }

    public CommodityDialog(@NonNull Context context, CommodityEntity entity) {
        super(context, R.style.MyDialog);
        this.context=context;
        commodity=entity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow=getWindow();
        dialogWindow.setGravity(Gravity.CENTER);

        View view=View.inflate(context,R.layout.dialog_commodity,null);
        ImageView background= view.findViewById(R.id.imv_commodity_dialog_background);
        ImageView exitImageView=view.findViewById(R.id.imv_commodity_dialog_exit);
        exitImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel();
            }
        });
        background.setOutlineProvider(new ViewOutlineProviderImpl(40));
        background.setClipToOutline(true);
        setContentView(view);

        initFind();
        initView();
        initButton();

        WindowManager window= ((Activity)context).getWindowManager();
        WindowMetrics metrics= window.getCurrentWindowMetrics();
        Rect rect= metrics.getBounds();

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = rect.width();
        lp.height=rect.height();
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }

    void initFind(){
        imageView=findViewById(R.id.imv_commodity_dialog_image);
        nameTextView=findViewById(R.id.txv_commodity_dialog_name);
        introductionTextView=findViewById(R.id.txv_commodity_dialog_introduction);
        collectButton=findViewById(R.id.btn_commodity_dialog_collect);
    }

    void initButton(){
        collectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new CollectCommodityRequest((Activity) context)
                        .setCollectButton(collectButton)
                        .setIsCollect(commodity)
                        .setCommodityId(commodity.id)
                        .setSuccessRunnable(new Runnable() {
                            @Override
                            public void run() {
                                outSideCollectButton.setText(commodity.isCollection?"取消收藏":"收藏");
                            }
                        });
                request.start();
            }
        });
    }

    void initView(){
        nameTextView.setText(commodity.title);
        introductionTextView.setText(commodity.description);
        collectButton.setText(String.valueOf(commodity.isCollection?"取消收藏":"收藏"));
        Glide.with(context).load(commodity.insidePictureSrc).placeholder(R.drawable.sample_picture).into(imageView);
    }

}
